package com.zx.BookSeaReader.ui.label;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.BookSeaReader.R;
import com.zx.BookSeaReader.base.BaseActivity;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookLabelBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.LabelService;
import com.zx.BookSeaReader.ui.login.LoginActivity;
import com.zx.BookSeaReader.ui.login.RegisterActivity;
import com.zx.BookSeaReader.util.TextHelper;
import com.zx.BookSeaReader.widget.RefreshLayout;
import com.zx.BookSeaReader.widget.itemdecoration.DividerItemDecoration;

import java.util.List;

public class LabelActivity extends BaseActivity {

    @BindView(R.id.refresh_layout)
    RefreshLayout mRlRefresh;
    @BindView(R.id.refresh_rv_content)
    RecyclerView mRvLabel;
    @BindView(R.id.tv_no_label)
    TextView mTvNoLabel;
    @BindView(R.id.tv_title_text)
    TextView mTvTitle;
    @BindView(R.id.ll_title_back)
    LinearLayout llTitleBack;

    private UserBean userBean;
    private Book book;
    private LabelService mLabelService;
    private List<BookLabelBean> Labels;
    private LabelAdapter mLabelAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_label);
        ButterKnife.bind(this);
        mLabelAdapter = new LabelAdapter();
        mLabelService=new LabelService();
        mTvTitle.setText("书签列表");
        userBean = (UserBean)getIntent().getSerializableExtra(APPCONST.USER);
        book=(Book)getIntent().getSerializableExtra(APPCONST.BOOK);
        Labels=mLabelService.getLabels(userBean.getUsername(),book.getId());

        //TextHelper.showText(Labels.size()+"");

        mRvLabel.setLayoutManager(new LinearLayoutManager(this));
        mRvLabel.addItemDecoration(new DividerItemDecoration(this));
        mRvLabel.setAdapter(mLabelAdapter);
        mRlRefresh.setBackground(ContextCompat.getDrawable(this,R.color.white));
        //mRlRefresh.setVisibility(View.GONE);
        mRlRefresh.setVisibility(View.VISIBLE);
        mRlRefresh.showLoading();
        mLabelAdapter.refreshItems(Labels);
        mRlRefresh.showFinish();
        if (Labels.size() == 0){
            mTvNoLabel.setVisibility(View.VISIBLE);
        }
        else {
            mTvNoLabel.setVisibility(View.GONE);
        }

        if (!(mRvLabel.getAdapter() instanceof LabelAdapter)){
            mRvLabel.setAdapter(mLabelAdapter);
        }

        initClick();

    }
    protected void initClick() {

        llTitleBack.setOnClickListener(
                (v) ->{
                    finish();
                }
        );

        //书签的点击事件
        mLabelAdapter.setOnItemClickListener(
                (view, pos) -> {
                    BookLabelBean bookLabelBean = mLabelAdapter.getItem(pos);
                    Intent intent = new Intent();
                    intent.putExtra(APPCONST.LABEL, bookLabelBean);
                    this.setResult(RESULT_OK, intent);
                    this.finish();

                }
        );
        //书签的长点击事件
        mLabelAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openItemDialog(mLabelAdapter.getItem(pos));
                    return true;
                }
        );
    }

    private void openItemDialog(BookLabelBean labelBean) {
        String[] menus;
        menus = getResources().getStringArray(R.array.menu_long_click);
        AlertDialog labelBeanDialog = new AlertDialog.Builder(this)
                .setTitle(labelBean.getTitle())
                .setAdapter(new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1, menus),
                        (dialog, which) -> onItemMenuClick(menus[which], labelBean))
                .setNegativeButton(null, null)
                .setPositiveButton(null, null)
                .create();

        labelBeanDialog.show();
    }
    private void onItemMenuClick(String which, BookLabelBean labelBean) {
        switch (which) {
            //编辑
            case "编辑":
                editLabel(labelBean);
                break;
            //删除
            case "删除":
                mLabelService.deleteLabel(labelBean);
                mLabelAdapter.removeItem(labelBean);
                break;
            default:
                break;
        }
    }
    /**
     * 编辑书签
     * @param
     */
    private void editLabel(BookLabelBean labelBean){
        final EditText editText = new EditText(this);
        AlertDialog.Builder editDialog = new AlertDialog.Builder(this);
        //editDialog.setIcon(R.mipmap.ic_launcher_round);
        editDialog.setTitle("编辑书签名称").setView(editText);
        editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String title = editText.getText().toString();
                labelBean.setTitle(title);
                mLabelService.updateLabel(labelBean);
                Labels=mLabelService.getLabels(userBean.getUsername(),book.getId());
                mLabelAdapter.refreshItems(Labels);
                //TextHelper.showText("输入的内容为：" + chapterName);
            }
        });
        editDialog.show();
    }

}
